package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.DesignerInfoimpl;
import com.ysd.dao.impl.ProductdetailsinfoDaoImpl;
import com.ysd.entity.DesignerInfo;
import com.ysd.entity.Productdetailsinfo;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminDesignerInfoSerclet extends BaseServlet {
	public void getDesignerInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DesignerInfoimpl pcDao = new DesignerInfoimpl();//创建实现类对象
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<DesignerInfo> pclist= pcDao.getDesignerInfoByPage(page, size);//查询所有分类
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		Gson gson = new Gson();
		String res = gson.toJson(pclist);//将pclist装华为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
	}
	public void getDesignerInfoBySize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DesignerInfoimpl pcDao = new DesignerInfoimpl();//创建实现类对象
		
		List<DesignerInfo> pclist= pcDao.getDesignerInfo();//查询所有分类
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		Gson gson = new Gson();
		String res = gson.toJson(pclist.size());//将pclist装华为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
	}
	public void delDesignerInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("designerid"));
		DesignerInfoimpl pcDao = new DesignerInfoimpl();//创建实现类对象
		int n= pcDao.deleteDesignerInfo(cId);
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		if (n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		out.flush();
		out.close();
	}
	public void adddesi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		DesignerInfoimpl pcDao = new DesignerInfoimpl();

		
		String designerName = request.getParameter("designerName");//名称
		String designerTitle = request.getParameter("designerTitle");//职位
		String designerImg = request.getParameter("designerImg");//图片

		//接受前台传来的参数
		if (designerName!=null&&!designerName.equals("")) {
			List<DesignerInfo> pcList = pcDao.getDesignerInfo(designerName);
			if (pcList.size()==0) {
				DesignerInfo pc = new DesignerInfo(0, designerName, designerTitle, designerImg);

				//调用构造方法，创建餐品分类的对象
				System.out.println(pc);//打印
				//创建实现类对象
				int n= pcDao.addproduct(pc);
				// 调用实现类的构造方法执行sql语句返回受影响行数
				if (n>0) {
					out.print(Result.toClient(true, "成功",""));
					//大于0成功
				} else {
					out.print(Result.toClient(false, "删除失败",""));
					//否则失败
				}
			} else {
				out.print(Result.toClient(false, "姓名重复",""));
			}
		}
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		//1创建SmartUpload对象
		SmartUpload smart = new SmartUpload();
		
		//2设置编码格式
		smart.setCharset("UTF-8");
		
		//3初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//4上传
			smart.upload();
			//5设置上传文件名字
			String fileName = System.currentTimeMillis() + ".jpg";
			//将文件保存到制定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			//响应
			out.print(Result.toClient(true, "上传成功",fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败","图片格式不符"));
		}
		out.flush();
		out.close();
	}
	public void getDesignerInfobyid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		DesignerInfoimpl pcDao = new DesignerInfoimpl();
		int cateId = Integer.parseInt(request.getParameter("cId"));
		DesignerInfo pc = pcDao.getDesignerInfobyid(cateId);
		Gson gson = new Gson();
		String res = gson.toJson(pc);
		out.print(res);
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void updateDesignerInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DesignerInfoimpl pcDao = new DesignerInfoimpl();
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		
		int designerid = Integer.parseInt(request.getParameter("designerid"));
		String designerName = request.getParameter("designerName");
		String designerTitle = request.getParameter("designerTitle");
		String designerImg = request.getParameter("designerImg");
		
		DesignerInfo pc = new DesignerInfo(designerid, designerName, designerTitle, designerImg);
		int n = pcDao.updateDesignerInfo(pc);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功",""));
		} else {
			out.print(Result.toClient(false, "修改失败",""));
		}
		out.flush();
		out.close();
	}

}
